<?php
namespace app\api\controller;

use app\model\AdminModel;
use think\facade\Request;
use think\facade\Validate;

class Login{
    //注册判断
    public function enroll_opinion(){
        $admin = Request::param();

        $validate = Validate::rule([
            'admin_name|用户名' => 'require|min:2|max:50',
            'admin_email|邮箱' => 'require|min:6|max:100',
            'admin_password|密码' => 'require|min:6|max:50',
            'password|确认密码' => 'require|min:6|max:50'
        ]);
        $check = $validate -> check($admin);
        if(!$check){
            $data = [
                'status'=> 10001,
                'message' => $validate->getError(),
                'date'=>''
            ];
            return json($data);
        }
        if($admin['admin_password'] !=$admin['password'] ){
            $data = [
                'status'=> 10002,
                'message' => '密码与确认密码不匹配' ,
                'date'=>''
            ];

            return json($data);
        }
        $useradmin = AdminModel::where('admin_email','=',$admin['admin_email'])->find();
        if($useradmin){
            $data = [
                'status'=> 10003,
                'message' => '该用户已经存在' ,
                'date'=>''
            ];
            return json($data);
        }
        $result = AdminModel::create([
            'admin_name' => $admin['admin_name'],
            'admin_email' => $admin['admin_email'],
            'admin_password' => $admin['admin_password'],
            'arr_time' => time()
        ]);
        if($result){
                $data = [
                    'status'=> 0,
                    'message' => '注册成功，返回登录' ,
                    'date'=>''
                ];
                return json($data);

        }else{
            $data = [
                'status'=> 10004,
                'message' => '奇妙错误' ,
                'date'=>''
            ];
            return json($data);
        }

    }

    //登录
    public function reg(){
        $admin = Request::param();
        $validate = Validate::rule([
            'admin_email|邮箱' => 'require|min:2|max:50',
            'admin_password|密码' => 'require|min:6|max:50',
            'enroll|验证码' => 'require|min:4|max:4'
        ]);
        $check = $validate->check($admin);
        if(!$check){
            $data = [
                'status'=> 10001,
                'message' => $validate->getError(),
                'date'=>[]
            ];
            return json($data);
        }
        $useradmin = AdminModel::where('admin_email','=',$admin['admin_email'])->find();

        if(empty($useradmin)){
            $data = [
                'status'=> 10002,
                'message' => '该用户不存在或前往注册',
                'date'=>[]
            ];
            return json($data);
        }
        session_start();
//        var_dump(stristr(implode($array),'4461'));
        $enroll = stristr(implode($_SESSION['array']),$admin['enroll']);
        if($admin['admin_email']==$useradmin['admin_email'] && $admin['admin_password']==$useradmin['admin_password'] && $enroll){
            $_SESSION['admin_email'] = $admin['admin_email'];
            $_SESSION['admin_name'] = $useradmin['admin_name'];
            $_SESSION['admin_id'] = $useradmin['admin_id'];
            $data = [
                'status'=> 0,
                'message' => '登录成功',
                'date'=>[$useradmin['admin_id']]
            ];
            return json($data);
        }else{
            $data = [
                'status'=> 10003,
                'message' => '密码或验证码错误',
                'date'=>[]
            ];
            return json($data);
        }



    }

}